package com.atguigu.eduservice.controller;


import cn.hutool.core.bean.BeanUtil;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.commonOrder.UCourseOrder;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author test.java
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

	@Resource
	private EduCourseService courseService;

	@Resource
	private EduTeacherService teacherService;

	@ApiOperation(value = "新增课程")
	@PostMapping("/save-course-info")
	public Result saveCourseInfo(
			@ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
			@RequestBody CourseInfoVo courseInfoForm){

		String courseId = courseService.saveCourseInfo(courseInfoForm);
		if(!StringUtils.isEmpty(courseId)){
			return Result.ok().data("courseId", courseId);
		}else{
			return Result.error().message("保存失败");
		}
	}


	//查询前8条热门课程，查询前4条名师
	@GetMapping("/index")
	public Result index() {
		//查询前8条热门课程
		QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("id");
		wrapper.last("limit 8");
		List<EduCourse> eduList = courseService.list(wrapper);

		//查询前4条名师
		QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
		wrapperTeacher.orderByDesc("id");
		wrapperTeacher.last("limit 4");
		List<EduTeacher> teacherList = teacherService.list(wrapperTeacher);

		return Result.ok().data("eduList",eduList).data("teacherList",teacherList);
	}



	@PostMapping("/getCourseOrder/{courseId}")
	public UCourseOrder getCourseOrderById(@PathVariable("courseId") String courseId){
		EduCourse course = courseService.getById(courseId);
		String teacherId = course.getTeacherId();
		//查询老师的姓名
		String teacherName = teacherService.selectTeacherNameById(teacherId);
		UCourseOrder uCourseOrder = BeanUtil.copyProperties(course, UCourseOrder.class);
		uCourseOrder.setTeacherName(teacherName);
		return uCourseOrder;
	}

}

